package com.cqie.hospitalescort.service.login.UserDetailService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqie.hospitalescort.DTO.AuthUser;
import com.cqie.hospitalescort.entity.EscortInfo;
import com.cqie.hospitalescort.entity.UserInfo;
import com.cqie.hospitalescort.enums.LoginTerminalEnum;
import com.cqie.hospitalescort.enums.RoleEnum;
import com.cqie.hospitalescort.exception.IllegalParameterException;
import com.cqie.hospitalescort.mapper.EscortInfoMapper;
import com.cqie.hospitalescort.mapper.UserInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WxUserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private EscortInfoMapper escortInfoMapper;

    /**
     * 加载用户信息，不进行认证功能和权限校验
     *
     * @param openid
     * @return
     */
    @Override
    public AuthUser loadUserByUsername(String openid) {
        if (openid.startsWith(LoginTerminalEnum.MINI_PROGRAM.getCode())) {
            openid = openid.substring((LoginTerminalEnum.MINI_PROGRAM.getCode() + "_").length());
        }

        UserInfo user = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getOpenid, openid));
        if (null == user) {
            throw new UsernameNotFoundException("微信用户不存在！");
        }
        AuthUser authUser = new AuthUser();

        if (RoleEnum.ESCORT.getRole() == user.getRole()) {
            //查询陪诊员详细信息表
            EscortInfo escortInfo = escortInfoMapper.selectById(user.getId());
            BeanUtils.copyProperties(escortInfo, authUser, "id", "createTime");
        }
        authUser.setId(user.getId());
        authUser.setOpenid(user.getOpenid());
        authUser.setPassword(user.getOpenid());
        authUser.setNickname(user.getNickname());
        authUser.setGender(user.getGender());
        authUser.setAvatarurl(user.getAvatarurl());
        authUser.setTelnum(user.getTelnum());
        authUser.setRole(user.getRole());
        authUser.setCreateTime(user.getCreateTime());
        authUser.setUpdateTime(Objects.nonNull(authUser.getUpdateTime()) ? authUser.getUpdateTime() : user.getUpdateTime());
        return authUser;
    }

    /**
     * 角色区分
     *
     * @param role
     * @return
     */
    public static Set<GrantedAuthority> getAuthorities(Integer role) {
        Set<GrantedAuthority> authorities = new HashSet<>();

        switch (role) {
            case 0:
                authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));  //管理员
                break;
            case 1:
                authorities.add(new SimpleGrantedAuthority("ROLE_USER"));  //用户
                break;
            case 2:
                authorities.add(new SimpleGrantedAuthority("ROLE_ESCORT"));  //陪诊员
                break;
            default:
                throw new IllegalParameterException("错误系统角色！");
        }
        return authorities;
    }
}
